package it.gameplanet.client.widgets;

import it.gameplanet.client.utils.AbstractAsyncCallback;
import it.gameplanet.model.Articolo;
import it.gameplanet.model.Categoria;
import it.gameplanet.service.GamePlanetService;

import java.util.List;

import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

public class GamePlanetTree extends LazyPanel {

	public static final String LEAF = "LEAF";

	public GamePlanetTree() {
	}

	private Tree createDynamicTree() {
		// Create a new tree
		Tree dynamicTree = new Tree();

		TreeItem videoGiochiPS3 = dynamicTree.addItem(new MasterCategoriaTreeItem("PS3"));
		videoGiochiPS3.addItem(LEAF);
		
		TreeItem videoGiochiXB360 = dynamicTree.addItem(new MasterCategoriaTreeItem("XBOX360"));
		videoGiochiXB360.addItem(LEAF);

		TreeItem videoGiochiWii = dynamicTree.addItem(new MasterCategoriaTreeItem("Wii"));
		videoGiochiWii.addItem(LEAF);
		
		TreeItem actionFigures = dynamicTree.addItem(new MasterCategoriaTreeItem("Actions Figures"));
		actionFigures.addItem(LEAF);

		TreeItem robotz = dynamicTree.addItem(new MasterCategoriaTreeItem("Robot"));
		robotz.addItem(LEAF);

		TreeItem film = dynamicTree.addItem(new MasterCategoriaTreeItem("Film"));
		film.addItem(LEAF);

		// Add a handler that automatically generates some children
		dynamicTree.addOpenHandler(new OpenHandler<TreeItem>() {

			public void onOpen(OpenEvent<TreeItem> event) {
				final TreeItem item = event.getTarget();
				if (item.getWidget() instanceof MasterCategoriaTreeItem && 
						LEAF.equals(item.getChild(0).getText()) && 
						item.getChildCount() == 1) {
						
					populateTreeItemChildren(item);
				}
				else if (item.getWidget() instanceof CategoriaTreeItem && 
					LEAF.equals(item.getChild(0).getText()) && 
					item.getChildCount() == 1) {
					
					populateArticoli(item);
				}
			}


		});

		// Return the tree
		return dynamicTree;
	}
	
	private void populateTreeItemChildren(final TreeItem parentTreeItem) {
		// Close the item immediately
		parentTreeItem.setState(false, false);

		GamePlanetService.Utils.getGamePlanetServiceAsync().findCategorieByParent(parentTreeItem.getText(), new AbstractAsyncCallback<List<Categoria>>() {

			@Override
			public void onSuccess(List<Categoria> result) {
				for(Categoria categoria: result) {
					parentTreeItem.addItem(new CategoriaTreeItem(categoria)).addItem(LEAF);
					
				}

				// Remove the temporary item when we
				// finish
				// loading
				parentTreeItem.getChild(0).remove();

				// Reopen the item
				parentTreeItem.setState(true, false);					
			}
			
		});
	}
		
	private void populateArticoli(final TreeItem item) {
		CategoriaTreeItem categoria = (CategoriaTreeItem) item.getWidget();

		// Close the item immediately
		item.setState(false, false);

		// Call the service for Articolo with this Categoria
		GamePlanetService.Utils.getGamePlanetServiceAsync()
				.findArticoliByCategoria(categoria.getCategoria(),
						new AbstractAsyncCallback<List<Articolo>>() {

							@Override
							public void onSuccess(
									List<Articolo> result) {
								// Add a random number of children
								// to the
								// item
								for (Articolo articolo : result) {
									item
											.addItem(new ArticoloTreeItem(
													articolo));
								}

								// Remove the temporary item when we
								// finish
								// loading
								item.getChild(0).remove();

								// Reopen the item
								item.setState(true, false);
							}

						});
	}	
	
	public void reload() {

		setWidget(createDynamicTree());
	}

	public TreeItem getItem(final String categoria) {

		if( categoria==null ) {
			throw new IllegalArgumentException("categoria cannot be null in GamePlanetTree.getItem");
		}
		
		Tree tree = (Tree) getWidget();
		
		for(int i=0;i<tree.getItemCount();i++) {
			if( categoria.equals(tree.getItem(i).getText()) ) {
				return tree.getItem(i);
			}
		}
		
		return null;
	}



	@Override
	protected Widget createWidget() {
		return createDynamicTree();
	}

}
